package domain

const (
	ClubFormStatusFlowTableName = "club_form_status_flows"
)

type ClubFormStatusFlowType int64

const (
	ClubFormStatusFlowTypeDefault ClubFormStatusFlowType = iota
	ClubFormStatusFlowTypeDraft
)

type ClubFormStatusFlow struct {
	ID               int64                  `json:"id"`
	ClubID           *int64                 `json:"-"`
	FormID           *int64                 `json:"-"`
	ParentID         *int64                 `json:"-"`
	Title            string                 `json:"title" gorm:"type:varchar(255);not null"`
	Description      *string                `json:"description,omitempty"`
	Type             ClubFormStatusFlowType `json:"type" gorm:"not null;default:0"`
	PrimaryColor     string                 `json:"primary_color" gorm:"not null;default:'#FFFFFF'"`
	IsDefaultSuccess bool                   `json:"is_default_success" gorm:"not null;default:false;"`
	IsDefaultFailure bool                   `json:"is_default_failure" gorm:"not null;default:false;"`
	Parent           *ClubFormStatusFlow    `json:"parent,omitempty" gorm:"foreignKey:ParentID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	Children         []*ClubFormStatusFlow  `json:"children" gorm:"foreignKey:ParentID;"`
	Club             *Club                  `json:"club,omitempty" gorm:"foreignKey:ClubID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	Form             *ClubForm              `json:"form,omitempty" gorm:"foreignKey:FormID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
}

type ClubFormStatusFlowDTO struct {
	ID               int64                    `json:"id"`
	ClubID           *int64                   `json:"-"`
	ParentID         *int64                   `json:"-"`
	Title            string                   `json:"title" gorm:"type:varchar(255);not null"`
	Description      *string                  `json:"description,omitempty"`
	Type             ClubFormStatusFlowType   `json:"type"`
	PrimaryColor     string                   `json:"primary_color" `
	IsDefaultSuccess bool                     `json:"is_default_success" gorm:"not null;default:false;"`
	IsDefaultFailure bool                     `json:"is_default_failure" gorm:"not null;default:false;"`
	Parent           *ClubFormStatusFlowDTO   `json:"parent,omitempty" gorm:"foreignKey:ParentID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	Children         []*ClubFormStatusFlowDTO `json:"children" gorm:"foreignKey:ParentID;"`
	Club             *ClubDTO                 `json:"club,omitempty" gorm:"foreignKey:ClubID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
}

func (f *ClubFormStatusFlowDTO) FromStatus(s *ClubFormStatusFlow) {
	f.ID = s.ID
	f.Title = s.Title
	f.Description = s.Description
	f.IsDefaultFailure = s.IsDefaultFailure
	f.IsDefaultSuccess = s.IsDefaultSuccess
	f.PrimaryColor = s.PrimaryColor
	f.Type = s.Type
	if s.Parent != nil {
		m := &ClubFormStatusFlowDTO{}
		m.FromStatus(s.Parent)
		f.Parent = m
	}
	if len(s.Children) != 0 {
		for _, child := range s.Children {
			m := &ClubFormStatusFlowDTO{}
			m.FromStatus(child)
			f.Children = append(f.Children, m)
		}
	}

}
